# xml文件的读取方式
import xml.etree.ElementTree as ET
import re
tree = ET.parse('01_1.xml')
root = tree.getroot()
#ElementTree是将xml文件当作树来读取，每一个tag相当与一个节点


def readAllpoint():
    '''

    :return: 遍历所有节点,只能读到第三层，对于第四层中的内容无法读取
    '''
    for child in root:
        for sub in child:
            print("sub.tag is {}, sub.attrib is {}, sub.text is {}".format(sub.tag,sub.attrib,sub.text))


def changeText():
    '''

    :return:根据遍历来修改内容
    '''
    label = re.compile('size')
    label_wid = re.compile('width')
    label_hei = re.compile('height')
    for child in root:
        if label.match(child.tag):
            for sub in child:
                if label_wid.match(sub.tag):#匹配width
                    new_wid =int(sub.text)*2
                    sub.text = str(new_wid)
                if label_hei.match(sub.tag):#匹配height
                    new_hei = int(sub.text)*2
                    sub.text = str(new_hei)
    tree.write('output.xml')
def changeTextSimple():
    '''

    :return: 使用函数查找
    '''
    for width in root.iter('width'):
        new_wid = int(width.text)*3
        width.text = str(new_wid)
    for height in root.iter('height'):
        new_hei = int(height.text)*3
        height.text = str(new_hei)
    tree.write('output2.xml')
if __name__ == '__main__':
    readAllpoint()
    changeText()
    changeTextSimple()